#include "LevelManager.h"

// Initialize the instance to null
LevelManager* LevelManager::m_LevelInstance = 0;

LevelManager::LevelManager(void)
{
}

LevelManager::~LevelManager(void)
{
}

LevelManager* LevelManager::GetInstance(void)
{
	// Create the instance if it doesn't exist
	if(!m_LevelInstance)
		m_LevelInstance = new LevelManager;

	// Return the singleton
	return m_LevelInstance;
}

void LevelManager::DeleteLevelInstance(void)
{
	// If instance exist, delete it!
	if(m_LevelInstance)
	{
		delete m_LevelInstance;
		m_LevelInstance = NULL;
	}
}

void LevelManager::Initialize(void)
{
	// Get the instances of the classes we need
	m_pModelMan = ModelManager::GetInstance();
	m_PowerUps = PowerUps::GetInstance();
	m_pVirusEn = VirusEnemy::GetInstance();
	m_pMiniGame = MiniGame::GetInstance();
	m_pObstacleMan = ObstacleManager::GetInstance();
	m_pCamera = Camera::GetInstance();
}

void LevelManager::LoadLevel(int iLevel)
{
	// Chars to compare what is
	// received from the text file
	// to what can be loaded
	char cModel[] = "model";
	char cMines[] = "mines";
	char cPowerups[] = "powerups";
	char cTesla[] = "tesla";
	char cPiston[] = "piston";
	char cAcid[] = "acid";
	char cFirewall[] = "firewall";
	char cMinigame1[] = "minigame1";
	char cMinigame2[] = "minigame2";
	char cMinigame3[] = "minigame3";
	char cCamBounds[] = "camera";
	sMiniGameCount = 0;

	ifstream levelFile;

	string strNum;
	ostringstream convert;
	convert << iLevel;
	strNum = convert.str();
	string fileName = "Levels\\Level" + strNum + ".txt";

	sPreModels = 0;
	sPrePowerUps = 0;
	sPreTeslaCoil = -1;
	sPrePiston = -1;
	sPreAcidPit = -1;
	sPreFirewall = -1;

	levelFile.open(fileName.c_str());

	// Reading from file
	if(levelFile.is_open())
	{
		while(!levelFile.eof())
		{
			levelFile >> cObject;
			if(!strcmp(cObject, cModel))
			{
				levelFile >> cFilename >> fFirstIn >> fSecondIn >> fThirdIn >> fFourthIn;
				sPreModels = m_pModelMan->LoadModel((LPCSTR)cFilename, D3DXVECTOR3(fFirstIn, fSecondIn, fThirdIn), fFourthIn);
			}
			if(!strcmp(cObject, cPowerups))
			{
				levelFile >> fFirstIn;
				sPrePowerUps = m_PowerUps->CreatePowerUps((short)fFirstIn);
			}
			if(!strcmp(cObject, cMines))
			{
				levelFile >> fFirstIn;
				m_pVirusEn->CreateMines((short)fFirstIn);
			}
			if(!strcmp(cObject, cMinigame1))
			{
				levelFile >> fFirstIn;
				sMiniGame1 = (short)fFirstIn;
				++sMiniGameCount;
			}
			if(!strcmp(cObject, cMinigame2))
			{
				levelFile >> fFirstIn;
				sMiniGame2 = (short)fFirstIn;
				++sMiniGameCount;
			}
			if(!strcmp(cObject, cMinigame3))
			{
				levelFile >> fFirstIn;
				sMiniGame3 = (short)fFirstIn;
				++sMiniGameCount;
			}
			if(!strcmp(cObject, cTesla))
			{
				levelFile >> fFirstIn >> fSecondIn >> fThirdIn;
				sPreTeslaCoil = m_pObstacleMan->CreateTeslaCoil(D3DXVECTOR3(fFirstIn, fSecondIn, fThirdIn));
			}
			if(!strcmp(cObject, cPiston))
			{
				levelFile >> fFirstIn >> fSecondIn >> fThirdIn;
				sPrePiston = m_pObstacleMan->CreatePiston(D3DXVECTOR3(fFirstIn, fSecondIn, fThirdIn));
			}
			if(!strcmp(cObject, cAcid))
			{
				levelFile >> fFirstIn >> fSecondIn >> fThirdIn;
				sPreAcidPit = m_pObstacleMan->CreateAcidPit(D3DXVECTOR3(fFirstIn, fSecondIn, fThirdIn));
			}
			if(!strcmp(cObject, cFirewall))
			{
				levelFile >> fFirstIn >> fSecondIn >> fThirdIn;
				sPreFirewall = m_pObstacleMan->CreateFirewall(D3DXVECTOR3(fFirstIn, fSecondIn, fThirdIn));
			}
			if(!strcmp(cObject, cCamBounds))
			{
				levelFile >> fFirstIn >> fSecondIn >> fThirdIn >> fFifthIn;
				m_pCamera->SetBoundaries(fFirstIn, fSecondIn, fThirdIn, fFifthIn);
			}
		}
	}
}

////////////////////////////////////////////////////
// Preloaded functions (What is in the text file?)
short LevelManager::GetPreModels(void)
{
	return sPreModels;
}

short LevelManager::GetPowerUps(void)
{
	return sPrePowerUps;
}

short LevelManager::GetMiniGameID1(void)
{
	return sMiniGame1;
}

short LevelManager::GetMiniGameID2(void)
{
	return sMiniGame2;
}

short LevelManager::GetMiniGameID3(void)
{
	return sMiniGame3;
}

short LevelManager::GetPreTeslaCoil(void)
{
	return sPreTeslaCoil;
}

short LevelManager::GetPrePiston(void)
{
	return sPrePiston;
}

short LevelManager::GetPreAcidPit(void)
{
	return sPreAcidPit;
}

short LevelManager::GetPreFirewall(void)
{
	return sPreFirewall;
}
////////////////////////////////////////////////////

short LevelManager::GetMiniGameCount(void)
{
	return sMiniGameCount;
}